package com.voxel.sense.infomsg.view.menu;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.view.adapter.TBaseAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.interfaces.listener.onMyDialogItemClickListener;
import com.voxel.sense.infomsg.utils.GlideUtils;
import com.voxel.sense.infomsg.view.MenuBean;

import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.Factory;

/**
 * Created by chenyiyao on 2017/11/3.
 */

public class MultipleSelectAdapter extends TBaseAdapter<MenuBean> {
    private int mAllItemIconRes;//选中全部item 的图标
    private boolean mAddSelectAllItem = false; //一个选中全部的item
    private boolean mIsVisibleIcon = false;//是否显示item图标
    private onMyDialogItemClickListener mMyItemClickListener;


    public MultipleSelectAdapter(List<MenuBean> mMenuBeanList, Context context, int layoutID,
                                 boolean addSelectAllItem, int mAllItemIconRes,
                                 boolean mIsVisibleIcon) {
        super(context, mMenuBeanList, layoutID);
        this.mIsVisibleIcon = mIsVisibleIcon;
        this.mAllItemIconRes = mAllItemIconRes;
        this.mAddSelectAllItem = addSelectAllItem;
        if (mAddSelectAllItem)
            addSelectAllItem();
    }




    @Override
    public void initView(ViewHolder holder) {
        holder.addView(R.id.iv_select_icon);
        holder.addView(R.id.iv_desc_icon);
        holder.addView(R.id.tv_desc);
        holder.addView(R.id.ll_item);
    }

    @Override
    public void setViewValue(ViewHolder holder, int position) {
        TextView tv_desc = holder.getTextView(R.id.tv_desc);//描述信息
        ImageView iv_select_icon = holder.getImageView(R.id.iv_select_icon);//选择状态icon
        ImageView iv_desc_icon = holder.getImageView(R.id.iv_desc_icon);//item的icon
        View ll_item = holder.getView(R.id.ll_item);//item的icon
        MenuBean bean = list.get(position);
        boolean select = bean.isSelect();
        tv_desc.setText(bean.getmText());
        if (select) {//切换状态
            tv_desc.setTextColor(context.getResources().getColor(R.color.menu_selected));
            GlideUtils.GlideLoadPicNoDefault(context, R.mipmap.map_tip_select_icon, iv_select_icon);
        } else {//未选状态
            tv_desc.setTextColor(context.getResources().getColor(R.color.menu_unselected));
            GlideUtils.GlideLoadPicNoDefault(context, R.mipmap.map_tip_unselect_icon, iv_select_icon);
        }

        switch(bean.getmText()){
            case "现场巡查":
                iv_desc_icon.setImageDrawable(Factory.app().getResources().getDrawable(R.mipmap.ic_level_map_a));
                break;
            case "运维巡查":
                iv_desc_icon.setImageDrawable(Factory.app().getResources().getDrawable(R.mipmap.ic_level_map_b));
                break;
            case "质量控制":
                iv_desc_icon.setImageDrawable(Factory.app().getResources().getDrawable(R.mipmap.ic_level_map_c));
                break;
            case "应急响应":
                iv_desc_icon.setImageDrawable(Factory.app().getResources().getDrawable(R.mipmap.ic_level_map_d));
                break;
            case "监察执法":
                iv_desc_icon.setImageDrawable(Factory.app().getResources().getDrawable(R.mipmap.ic_level_map_e));
                break;
        }
        if (mIsVisibleIcon) {//是否显示icon
            if (mAddSelectAllItem) {
                //添加了一个item,所有要进行判断
                if (position == 0) {
                    iv_desc_icon.setVisibility(View.VISIBLE);
//                    iv_desc_icon.setImageDrawable(Factory.app().getResources().getDrawable(list.get(position).getNativeIcon()));
//                    GlideUtils.GlideLoadPicNoDefault(context, mAllItemIconRes, iv_desc_icon);
                } else {
                    iv_desc_icon.setVisibility(View.VISIBLE);
//                    iv_desc_icon.setImageDrawable(Factory.app().getResources().getDrawable(list.get(position).getNativeIcon()));
//                    GlideUtils.GlideLoadPicNoDefault(context, list.get(position).getmIconResId(), iv_desc_icon);

                }
            } else {
                iv_desc_icon.setVisibility(View.VISIBLE);
//                iv_desc_icon.setImageDrawable(Factory.app().getResources().getDrawable(list.get(position).getNativeIcon()));
//                GlideUtils.GlideLoadPicNoDefault(context, list.get(position).getmIconResId(), iv_desc_icon);
            }

        } else {
            iv_desc_icon.setVisibility(View.GONE);
        }

        //点击事件
        ItemClickEvent(position, ll_item, iv_select_icon, tv_desc);
    }

    private void ItemClickEvent(final int position, View root, final ImageView select_icon, final TextView tv_desc) {
        root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MenuBean bean = list.get(position);
                //点击按钮后，状态取反
                boolean select = !bean.isSelect();
                if (mAddSelectAllItem) {//有一个选中全部的item
                    if (position == 0) {
                        for (MenuBean m : list) {
                            m.setSelect(select);//改变所有的按钮
                        }
                    } else {
                        MenuBean firstItem = list.get(0);
//                        //由于第一项是全部按钮，若被选中时，当再点击其它项时，都应该还原状态
                        if (firstItem.isSelect()) {
                            firstItem.setSelect(false);
                        }
                        //点击按钮，改变状态
//                        refreshIcon(holder, select, position);
                        list.get(position).setSelect(select);
                    }
                    notifyDataSetChanged();
                } else {
                    //点击按钮，改变状态
                    refreshIcon(select, select_icon, tv_desc);//改变单个按钮
                    list.get(position).setSelect(select);
                }
                if (mMyItemClickListener != null) {
                    mMyItemClickListener.onItemClick(position, bean);
                }
            }
        });
    }

    private void refreshIcon(Boolean aBoolean, ImageView select_icon, TextView tv_desc) {
        if (aBoolean) {//切换状态
            tv_desc.setTextColor(context.getResources().getColor(R.color.menu_selected));
            GlideUtils.GlideLoadPic(context, R.mipmap.map_tip_select_icon, select_icon);
        } else {//未选状态
            tv_desc.setTextColor(context.getResources().getColor(R.color.menu_unselected));
            GlideUtils.GlideLoadPic(context, R.mipmap.map_tip_unselect_icon, select_icon);
        }
    }

    public void setOnMyItemClickListener(onMyDialogItemClickListener listener) {
        this.mMyItemClickListener = listener;
    }


    public void addSelectAllItem() {
        MenuBean bean = new MenuBean("全部", mAddSelectAllItem + "", true);
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        this.list.add(0, bean);
    }

    public int getAllItemIconRes() {
        return mAllItemIconRes;
    }

    public void setAllItemIconRes(int allItemIconRes) {
        mAllItemIconRes = allItemIconRes;
    }

    @Override
    public void setList(List<MenuBean> list) {
        this.list.clear();
        this.list.addAll(list);
        if (mAddSelectAllItem)
            addSelectAllItem();
        notifyDataSetChanged();
    }

    //获取选中的数据
    public List getSelectListData() {
        ArrayList list = new ArrayList();
        if (mAddSelectAllItem) {
            //添加了一个默认选全部的item
            int size = this.list.size();
            for (int i = 1; i < size; i++) {
                MenuBean menuBean = this.list.get(i);
                if (menuBean.isSelect()) {
                    list.add(menuBean);
                }
            }
        } else {
            //没有添加了一个默认选全部的item
            for (MenuBean bean : this.list) {
                if (bean.isSelect()) {
                    list.add(bean);
                }
            }
        }
        return list;
    }

}
